interface Capsule {
    id: string
    name: string
    email: string
    time: string
    content: string
    tip: string
}

let s:Capsule

function randcode() {
    let str = "";
    for (let i = 0; i < 2; i++) {
        str += String.fromCharCode(Math.floor(Math.random() * 26) + "a".charCodeAt(0));
    }
    return str;
}



function checkemail(str:string) {
    let Expression = /^\w{3,}(\.\w+)*@[A-z0-9]+(\.[A-z]{2,5}){1,2}$/;

    if (Expression.test(str) == true) {
        return true;
    } else {
        return false;
    }
}

function GetFormElement() {
    let capsule:Capsule = {id: '', name: '', email: '',time: '',content: '', tip:''}
    let name:HTMLInputElement = document.querySelector('.name')
    let name_value = name.value
    let mail:HTMLInputElement = document.querySelector('.e-mail')
    let mail_value = mail.value
    let time:HTMLInputElement = document.querySelector('.time')
    let time_value = time.value
    let nr:HTMLInputElement = document.querySelector('.neirong')
    let nr_value = nr.value
    let tip:HTMLInputElement = document.querySelector('.tip')
    let tip_value = tip.value
    if ( name_value.length !== 0 )
        capsule.name = name_value
    if ( mail_value.length !== 0 )
        capsule.email = mail_value
    if ( time_value.length !== 0 )
        capsule.time = time_value
    if ( nr_value.length !== 0 )
        capsule.content = nr_value
    // if ( tip_value.length !== 0 )
    capsule.tip = tip_value
    capsule.id = Math.round(100 * Math.random())+ '' + randcode() + '-' +  Math.round(100 * Math.random()) + '' + randcode() + '-' + randcode() + '' +  Math.round(100 * Math.random()) + '-' + randcode() + '' +  Math.round(100 * Math.random());
    s = capsule
    // localStorage.setItem('key',capsule.id)
    if (( checkemail(mail_value) === true) &&( name_value.length !== 0 ) && ( mail_value.length !== 0 ) && ( time_value.length !== 0 ) && ( nr_value.length !== 0 )){{
            localStorage.setItem('key',capsule.id)
            return capsule
        }
    }else {
        return null
    }

}

function write(value: any) {
    let key:string 
    key = value.id
    if (key){
        value = JSON.stringify(value)
    }
    localStorage.setItem(key,value)
}

function output() {
    let con:HTMLInputElement = document.querySelector('.content')
    let str:string = localStorage.getItem('key')
    con.value = str
}

function put() {
    let c:Capsule | null = GetFormElement()
    if (c !== null){
        write(c)
        window.location.href = "Put_done.html"
        return false
    }else {
        err()
        return false
    }
}

function err() {
    let name:HTMLInputElement = document.querySelector('.name')
    let name_value = name.value
    let mail:HTMLInputElement = document.querySelector('.e-mail')
    let mail_value = mail.value
    let nr:HTMLInputElement = document.querySelector('.neirong')
    let nr_value = nr.value
    let ename:HTMLElement = document.querySelector('.err_name')
    let email:HTMLElement = document.querySelector('.err_mail')
    let enr:HTMLElement = document.querySelector('.err_nr')

    if (!checkemail(mail_value)){
        if (mail_value.length !== 0){
            email.innerText = "邮箱 必须是一个有效的电子邮箱地址."
        }else {
            email.innerText = '邮箱 必须填写.  123'
        }
    }

    if (name_value.length === 0)
        ename.style.display = 'block'
    else
        ename.style.display = 'none'
    if (mail_value.length === 0)
        email.style.display = 'block'
    else{
        if (!checkemail(mail_value)){
            email.innerText = "邮箱 必须是一个有效的电子邮箱地址."
            email.style.display = 'block'
        }else {
            email.innerText = '邮箱 必须填写.  123'
            email.style.display = 'none'
        }
    }

    if (nr_value.length === 0)
        enr.style.display = 'block'
    else
        enr.style.display = 'none'
}

function getID() {
    let con:HTMLInputElement = document.querySelector('.key')
    let str:string = con.value
    return str
}

function ope(){
    add(read(getID()))
}

function add(s:Capsule) {
    // let s:Capsule = JSON.parse(localStorage.getItem('capsule'))
    // localStorage.removeItem('capsule')
    if (getID() !== ""){
        if (document.querySelector('.pill' ) === null ) {
            if (s !== null){
                if ( (Date.now() - (new Date(s.time)).getTime()) >= 0  ) {
                    let main = document.querySelector('.main')
                    let pill = document.createElement('div')
                    pill.className = 'pill'
                    let title = document.createElement('h2')
                    title.className = 'neirong'
                    title.style.marginTop = '50px'
                    title.style.marginBottom = '30px'
                    title.innerText = s.name + ' 在 ' + s.time + ' 对你说: '
                    let div = document.createElement('div')
                    div.style.padding = '15px 20px 15px 20px'
                    div.style.border = '1px solid #ff0033'
                    let con = document.createElement('pre')
                    con.style.lineHeight = '1.8em'
                    con.innerText = s.content
                    div.appendChild(con)
                    pill.appendChild(title)
                    pill.appendChild(div)
                    main.appendChild(pill)
                } else {
                    let main = document.querySelector('.main')
                    let pill = document.createElement('div')
                    pill.className = 'pill'
                    let title = document.createElement('h2')
                    title.className = 'neirong'
                    title.style.marginTop = '50px'
                    title.style.marginBottom = '30px'
                    title.innerText = '这颗胶囊未到提取时间，不能打开。'
                    let p = document.createElement('p')
                    p.className = 'ddl'
                    let b = document.createElement('b')
                    p.appendChild(b)
                    b.innerText = s.time
                    let p1 = document.createElement('span')
                    p1.innerText = '打开时间在 '
                    let p2 = document.createElement('span')
                    p2.innerText = ' ，距离现在'
                    let time = document.createElement('b')
                    time.className = 'every'
                    p.appendChild(p1)
                    p.appendChild(b)
                    p.appendChild(p2)
                    settime(s)

                    p.appendChild(time)
                    let sec = document.createElement('span')
                    p.appendChild(sec)
                    sec.innerText = '秒'
                    settime(s)
                    p.style.lineHeight = '1.8em'
                    p.style.fontSize = '13px'
                    let div = document.createElement('div')
                    div.style.padding = '15px 20px 15px 20px'
                    div.style.border = '1px solid #ff0033'
                    let con = document.createElement('pre')
                    con.style.lineHeight = '1.8em'
                    con.innerText = s.name + ' 给你留的提示信息' + s.tip
                    div.appendChild(con)
                    pill.appendChild(title)
                    pill.appendChild(p)
                    pill.appendChild(div)
                    main.appendChild(pill)
                }
            }else {
                let main = document.querySelector('.main')
                let div = document.createElement('div')
                div.className = 'pill tips-highlight'
                div.innerText = '没有这个胶囊Key'
                div.style.padding = '8px'
                div.style.width = '400px'
                div.style.color = '#900'
                main.appendChild(div)
            }
        }
        else {
            if (s === null){
                let main = document.querySelector('.main')
                let pill = document.querySelector('.pill')
                main.removeChild(pill)
                let div = document.createElement('div')
                div.className = 'pill tips-highlight'
                div.innerText = '没有这个胶囊Key'
                div.style.padding = '8px'
                div.style.width = '400px'
                div.style.color = '#900'
                main.appendChild(div)
            }
        }
    }else {
        let main = document.querySelector('.main')
        let pill = document.querySelector('.pill')
        let div = document.querySelector('.tips-highlight')
        main.removeChild(div)
        main.removeChild(pill)
    }
}

function settime(s:Capsule) {
    setInterval(() => {
        let t:number =  (new Date(s.time)).getTime() - Date.now()
        let time:HTMLElement = document.querySelector('.every')
        if (time === null){
            time.innerText = t.toString()
        }else {
            time.innerText = t.toString()
        }

    },1000)
}

function read(key: string) {
    let value: string = localStorage.getItem(key);
    if (value && value !== "undefined" && value !== "null") {
        return JSON.parse(value);
    }else
        return null;
}

function test() {
    let capsule:Capsule = {id: '123', name: '12', email: '1',time: '2',content: '3', tip:'4'}
    // capsule.id = ids
    localStorage.setItem('key',capsule.id)
    write(capsule)
    let x:Capsule = read(capsule.id)
    if (x !== null){
        console.log(x.name)
        console.log(x.time)
        console.log(x.content)
    }
}

function getNowDate(): string {
    const date = new Date();
    let month: string | number = date.getMonth() + 1;
    let strDate: string | number = date.getDate();
    let hour: string | number = date.getHours();
    let min: string | number = date.getMinutes();
    let sec: string | number = date.getSeconds();

    if (month <= 9) {
        month = "0" + month;
    }

    if (strDate <= 9) {
        strDate = "0" + strDate;
    }

    if (hour <= 9)
        hour = "0" + hour;

    if (min <= 9)
        min = "0" + min;

    if (sec <= 9)
        sec = "0" + sec;

    return date.getFullYear() + "-" + month + "-" + strDate + " "
        + hour + ":" + min + ":" + sec;
}

// function getNowDate() {
//     let tenMinsLate = new Date().getTime() + 600000
//     return moment(tenMinsLate).format('YYYY-MM-DD HH:mm:ss')
// }

function writetime() {
    let time:HTMLInputElement = document.querySelector('.time')
    time.value = getNowDate()
}

